package com.zz.service.iml;

import com.zz.dao.DrugDao;
import com.zz.dto.DrugResult;
import com.zz.pojo.Drug;
import com.zz.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Copyright Zz
 * FileName: DrugServiceIml
 * Author:   Zz
 * Date:     2023/6/1020:53
 * Description:药品表业务层实现类
 * hISTORY:
 * <author>  <time>     <version>    <desc>
 * 作者姓名   修改时间       版本号       描述
 */
@Service
@Transactional
public class DrugServiceIml implements DrugService {
    @Autowired
    private DrugDao drugDao;

    @Override
    public DrugResult queryDrugList(Map<String, Integer> map) {
        DrugResult drugResult = new DrugResult();
        List<Drug> list = drugDao.queryDrugList(map);
        drugResult.setDrugData(list);
        drugResult.setCurrentPageSize(list.size());
        int countDrug = drugDao.countDrug();
        drugResult.setPageCount((int) (Math.ceil(countDrug / (double) Integer.parseInt(String.valueOf(map.get("pageSize"))))));
        return drugResult;
    }

    @Override
    public DrugResult queryDrugByKey(Map<String, Object> map) {
        DrugResult drugResult = new DrugResult();
        List<Drug> list = drugDao.queryDrugByKey(map);
        drugResult.setDrugData(list);
        drugResult.setCurrentPageSize(list.size());
        int countDrug = drugDao.countDurgByKey(map);
        drugResult.setPageCount((int) (Math.ceil(countDrug / (double) Integer.parseInt(String.valueOf(map.get("pageSize"))))));
        return drugResult;
    }

    @Override
    public int insertDrug(Drug drug) {
        if (drugDao.queryDrugByNumber(drug.getDrugNumber()) != null)
            return 2;
        else return drugDao.insertDrug(drug);
    }

    @Override
    public int updateDrug(Drug drug) {
        return drugDao.updateDrug(drug);
    }

    @Override
    public int deleteDrug(String drugNumber) {
        return drugDao.deleteDrug(drugNumber);
    }
}
